package com.local.coupon.service.impl;

import com.local.common.util.PageResultUtil;
import com.local.coupon.mapper.CouponMapper;
import com.local.coupon.mapper.UserCouponRecordMapper;
import com.local.coupon.pojo.Coupon;
import com.local.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    
    @Autowired
    private UserCouponRecordMapper userCouponRecordMapper;

    /**
     * 查询数据列表，支持翻页和搜索
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param couponName
     * @param couponType 优惠券赠送类型，如果是0则通用券，用户领取；如果是1，则是注册赠券；如果是2，则是优惠券码兑换；
     * @param couponStatus 优惠券状态，如果是0则是正常可用；如果是1则是过期; 如果是2则是下架。
     * @param startTime
     * @param endTime
     * @param isDeleted 0正常  1删除
     * @return
     */
    @Override
    public PageResultUtil<Coupon> selectCouponList(Integer page, Integer limit, String sort, String order, String couponName, Integer couponType, Integer couponStatus, String startTime, String endTime, Integer isDeleted) {
        Integer start = (page - 1) * limit;
        List<Coupon> coupons = couponMapper.selectCouponList(start, limit, sort, order, couponName, couponType, couponStatus, startTime, endTime, isDeleted);
        Integer totalCoupons = couponMapper.getTotalCoupons(couponName, couponType, couponStatus, startTime, endTime, isDeleted);
        PageResultUtil<Coupon> couponPageResultUtil = new PageResultUtil<>(coupons, totalCoupons, limit, page);
        return couponPageResultUtil;
    }


    /**
     * 通过id查询
     * @param couponId
     * @return
     */
    @Override
    public Coupon selectById(Integer couponId) {
        return couponMapper.selectById(couponId);
    }


    /**
     * 新增
     * @param coupon
     * @return
     */
    @Override
    public Integer save(Coupon coupon) {
        return couponMapper.save(coupon);
    }


    /**
     * 修改
     * @param coupon
     * @return
     */
    @Override
    public Integer update(Coupon coupon) {

        return couponMapper.update(coupon);
    }


    /**
     * 根据id逻辑删除
     * @param couponId
     * @return
     */
    @Override
    public Integer delete(Integer couponId) {
        return couponMapper.delete(couponId);
    }


    /**
     * 查询商品优惠券页面的数据列表
     * @param userId
     * @return
     */
    @Override
    public List<Coupon> selectMallCouponList(Integer userId) {
        List<Coupon> coupons = couponMapper.selectMallCouponList();
        return coupons;
    }

}
